Micron Document




Conditional (computer programming)
part 16/26 · 46.2 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
console.log("One person won!");
} else if (x < 2/3) {
console.log("Two people won!");
} else {
console.log("It's a three-way tie!");
}

Lambda calculus

In Lambda calculus, the concept of an if-then-else conditional can be expressed using the following expressions:

true = λx. λy. x
false = λx. λy. y
ifThenElse = (λc. λx. λy. (c x y))

1. true takes up to two arguments and once both are provided (see currying), it returns the first argument given.
2. false takes up to two arguments and once both are provided(see currying), it returns the second argument given.
3. ifThenElse takes up to three arguments and once all are provided, it passes both second and third argument to the first argument(which is a function that given two arguments, and produces a result). We expect ifThenElse to only take true or false as an argument, both of which project the given two arguments to their preferred single argument, which is then returned.

note: if ifThenElse is passed two functions as the left and right conditionals; it is necessary to also pass an empty tuple () to the result of ifThenElse in order to actually call the chosen function, otherwise ifThenElse will just return the function object without getting called.

In a system where numbers can be used without definition (like Lisp, Traditional paper math, so on), the above can be expressed as a single closure below:

((λtrue. λfalse. λifThenElse.
(ifThenElse true 2 3)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────